﻿#include "ByteArray.h"
#include "ui/UIHelper.h"
ByteArray::ByteArray(unsigned int mBufferSize)
{

	m_pContent = new char[mBufferSize];
	memset(m_pContent, 0, mBufferSize);
	m_nTotalSize = mBufferSize;
	m_nRdptr = 0;
	m_nWrPtr = 0;

	/////add by vincent 20101218/////////
	tag = -1;
	/////////////////////////////////////
}

ByteArray::ByteArray(const char* pszFileName, const char* pszMode)
{
	/*
	unsigned long  size = 0;
	const unsigned  char *reader = CCFileUtils::sharedFileUtils()->getFileData(pszFileName, pszMode, &size);
	//开辟流长度的空间
	m_pContent = new char[size];
	//拷贝源数据内容
	memcpy(m_pContent, reader, size);
	m_nTotalSize = size;
	m_nRdptr = 0;
	m_nWrPtr = 0;
	tag = -1;
	//删除源数据内容
	delete reader;
	reader = NULL;
	*/
}

ByteArray::ByteArray(const char* pszFileName, const char* pszFileNameCopy, const char* pszMode, bool local)
{
	/*
	unsigned long  size = 0;
	const unsigned  char *reader = NULL;
	if (!local)
	{
		reader = getFileData(pszFileNameCopy, pszMode, &size);
	}
	else
	{
		reader = CCFileUtils::sharedFileUtils()->getFileData(pszFileName, pszMode, &size);
	}

	if (!reader)
	{
		size = 0;
		reader = CCFileUtils::sharedFileUtils()->getFileData(pszFileName, pszMode, &size);
	}

	//开辟流长度的空间
	m_pContent = new char[size];
	//拷贝源数据内容
	memcpy(m_pContent, reader, size);
	m_nTotalSize = size;
	m_nRdptr = 0;
	m_nWrPtr = 0;
	tag = -1;
	//删除源数据内容
	delete reader;
	reader = NULL;
	*/
}

ByteArray::~ByteArray()
{
	//    BYTE_DELETE_ARRAY( m_pContent);
	//CCLOG("~ByteArray");
	CC_SAFE_DELETE_ARRAY(m_pContent);
}

void ByteArray::print()
{
	//    unsigned short *p = (unsigned short*)(rd_ptr() + 2);
}

void ByteArray::resize(unsigned int mLength)
{
	int tmpLength = this->length();
	char *tmpContent = m_pContent;

	m_nTotalSize += INCRSIZE;
	while (m_nTotalSize < mLength) {
		m_nTotalSize += INCRSIZE;
	}

	m_pContent = new char[m_nTotalSize];
	memset(m_pContent, 0, m_nTotalSize);
	memcpy(m_pContent, tmpContent, tmpLength);

	delete[] tmpContent;
	tmpContent = NULL;
}

char* ByteArray::base() const
{
	return m_pContent;
}

char* ByteArray::end(void) const
{
	return m_pContent + m_nTotalSize;
}

char* ByteArray::rd_ptr(void) const
{
	return m_pContent + m_nRdptr;
}

void ByteArray::rd_ptr(int n)
{
	m_nRdptr += n;
}

char* ByteArray::wr_ptr(void)
{
	return base() + m_nWrPtr;
}

void ByteArray::wr_ptr(int n)
{
	int len = this->space();
	if (len < n)
	{
		this->resize(n);
	}

	m_nWrPtr += n;
}

unsigned int ByteArray::space()
{
	return m_nTotalSize - m_nWrPtr;
}

int ByteArray::length() const
{
	return m_nWrPtr - m_nRdptr;
}

int ByteArray::copy(const char *buf, int n)
{
	int len = this->space();
	if (len < n)
	{
		this->resize(n);
	}

	memcpy(this->wr_ptr(), buf, n);
	this->wr_ptr(n);
	return 0;
}

void ByteArray::rd_skip(int mBytes)
{
	rd_ptr(mBytes);
}

int ByteArray::read_int()
{
	signed int n = sizeof(int);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_int]");
		return 0;
	}
	int w = 0;
	Read(&w);
	char conData[4] = { 0 };
	conData[3] = (char)(((w >> 24) & 0xFF));
	conData[2] = (char)(((w >> 16) & 0xFF));
	conData[1] = (char)(((w >> 8) & 0xFF));
	conData[0] = (char)((w & 0xFF));
	w = *((int *)conData);
	convertToByteStream(w);
	return w;
}

unsigned int ByteArray::read_uint()
{
	unsigned int n = sizeof(unsigned int);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_uint]");
		return 0;
	}
	unsigned int w = 0;
	Read(&w);
	char conData[4] = { 0 };
	conData[3] = (char)(((w >> 24) & 0xFF));
	conData[2] = (char)(((w >> 16) & 0xFF));
	conData[1] = (char)(((w >> 8) & 0xFF));
	conData[0] = (char)((w & 0xFF));
	w = *((unsigned int *)conData);
	convertToByteStream(w);
	return w;
}

long long ByteArray::read_int64()
{
	signed int n = sizeof(long long);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_int64]");
		return 0;
	}
	long long w = 0;
	Read(&w);
	char conData[8] = { 0 };
	conData[7] = (char)((w >> 56) & 0xFF);
	conData[6] = (char)((w >> 48) & 0xFF);
	conData[5] = (char)((w >> 40) & 0xFF);
	conData[4] = (char)((w >> 32) & 0xFF);
	conData[3] = (char)((w >> 24) & 0xFF);
	conData[2] = (char)((w >> 16) & 0xFF);
	conData[1] = (char)((w >> 8) & 0xFF);
	conData[0] = (char)(w & 0xFF);
	w = *((long long *)conData);
	convertToByteStream(w);
	return w;
}

unsigned long long ByteArray::read_uint64()
{
	unsigned int n = sizeof(unsigned long long);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_uint64]");
		return 0;
	}
	unsigned long long w = 0;
	Read(&w);
	char conData[8] = { 0 };
	conData[7] = (char)((w >> 56) & 0xFF);
	conData[6] = (char)((w >> 48) & 0xFF);
	conData[5] = (char)((w >> 40) & 0xFF);
	conData[4] = (char)((w >> 32) & 0xFF);
	conData[3] = (char)((w >> 24) & 0xFF);
	conData[2] = (char)((w >> 16) & 0xFF);
	conData[1] = (char)((w >> 8) & 0xFF);
	conData[0] = (char)(w & 0xFF);
	w = *((unsigned long long *)conData);
	convertToByteStream(w);
	return w;
}

short ByteArray::read_int16()
{
	return read_short();
}

unsigned short ByteArray::read_uint16()
{
	return read_ushort();
}

short ByteArray::read_short()
{
	signed int n = sizeof(short);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_short]");
		return 0;
	}
	short w = 0;
	Read(&w);
	char conData[2] = { 0 };
	conData[1] = (char)(((w >> 8) & 0xFF));
	conData[0] = (char)((w & 0xFF));
	w = *((short *)conData);
	convertToByteStream(w);
	return w;
}

unsigned short ByteArray::read_ushort()
{
	unsigned int n = sizeof(unsigned short);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_ushort]");
		return 0;
	}
	unsigned short w = 0;
	Read(&w);
	char conData[2] = { 0 };
	conData[1] = (char)(((w >> 8) & 0xFF));
	conData[0] = (char)((w & 0xFF));
	w = *((unsigned short *)conData);
	convertToByteStream(w);
	return w;
}

long ByteArray::read_long()
{
	signed int n = sizeof(long);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_long]");
		return 0;
	}
	long w = 0;
	Read(&w);
	char conData[4] = { 0 };
	conData[3] = (char)((w >> 24) & 0xFF);
	conData[2] = (char)((w >> 16) & 0xFF);
	conData[1] = (char)((w >> 8) & 0xFF);
	conData[0] = (char)(w & 0xFF);
	w = *((long *)conData);
	convertToByteStream(w);
	return w;
}

unsigned long ByteArray::read_ulong()
{
	unsigned int n = sizeof(unsigned long);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_ulong]");
		return 0;
	}
	unsigned long w = 0;
	Read(&w);
	char conData[4] = { 0 };
	conData[3] = (char)((w >> 24) & 0xFF);
	conData[2] = (char)((w >> 16) & 0xFF);
	conData[1] = (char)((w >> 8) & 0xFF);
	conData[0] = (char)(w & 0xFF);
	w = *((unsigned long *)conData);
	convertToByteStream(w);
	return w;
}

signed char ByteArray::read_char()
{
	signed int n = sizeof(signed char);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_char]");
		return 0;
	}
	signed char w = 0;
	Read(&w);
	return w;
}

signed char ByteArray::read_byte()
{
	signed int n = sizeof(signed char);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_byte]");
		return 0;
	}
	signed char w = 0;
	Read(&w);
	return w;
}

unsigned char ByteArray::read_uchar()
{
	unsigned int n = sizeof(unsigned char);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_uchar]");
		return 0;
	}
	unsigned char w = 0;
	Read(&w);
	return w;
}

bool ByteArray::read_bool()
{
	unsigned int n = sizeof(bool);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_bool]");
		return 0;
	}
	bool w = 0;
	Read(&w);
	return w;
}

float ByteArray::read_float()
{
	unsigned int n = sizeof(float);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_float]");
		return 0;
	}
	float w = 0;
	Read(&w);
	return w;
}

double ByteArray::read_double()
{
	unsigned int n = sizeof(double);
	if (m_nRdptr + n>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_double]");
		return 0;
	}
	double w = 0;
	Read(&w);
	return w;
}

char* ByteArray::read_string(char* info, unsigned int len)
{
	//	char* result = (char*)memcpy(info, rd_ptr(), len);
	//	rd_ptr(len);
	//	return result;
	char* result = (char*)(memcpy(info, rd_ptr(), len));
	rd_ptr(len);
	return result;
}

std::string ByteArray::read_string()
{
	unsigned short len = read_short();
	if (m_nRdptr>m_nTotalSize) {
		CCLOG("exceed the byte streams size[read_string]");
		return "";
	}
	const int defaultLength = 65535;
	char tmpBuf[defaultLength] = { 0 };
	memset(tmpBuf, 0, defaultLength);
	memcpy(tmpBuf, rd_ptr(), len);
	rd_ptr(len);
	return std::string(tmpBuf);

	////#ifdef COM_JAVA
	//    int len = read_int();
	//    //len *=2;
	//    const int defaultLength = 65535;
	//	char tmpBuf[defaultLength]= {0};
	//	memset(tmpBuf, 0, defaultLength);
	//	this->memcpy(tmpBuf, rd_ptr(), len);
	//	
	//	rd_ptr(len);
	//	
	//    for (int i=0; i< len;) {
	//        char tmpChar = tmpBuf[i];
	//        tmpBuf[i] = tmpBuf[i+1];
	//        tmpBuf[i+1] = tmpChar;
	//        i +=2;
	//    }
	//    
	//	char tmpChar[defaultLength]= {0};
	//	memset(tmpChar, 0, defaultLength);
	//	int convertLength = CUnicodeConverter::UTF16Str_To_UTF8Str((const WORD*)(tmpBuf),(BYTE *)tmpChar);
	//	if (convertLength == 0) {
	//		printf("COM_JAVA ByteArray::read_wstring error read string length =%d failed \n",len);
	//        return std::string("");
	//	}
	//	return std::string(tmpChar);

	//#else
	//	unsigned short len = read_uint16(false);
	//#endif

	//	std::string strValue(rd_ptr(), len);
	//	rd_ptr(len);
	//	return strValue;
}

std::string ByteArray::read_wstring()
{
	/*
	unsigned short len = read_short();
	if (m_nRdptr + len>m_nTotalSize) {
		CCLOG("exceed the byte stream size[read_wstring]");
		return 0;
	}
	const int defaultLength = 65535;
	char tmpBuf[defaultLength] = { 0 };
	memset(tmpBuf, 0, defaultLength);
	memcpy(tmpBuf, rd_ptr(), len);

	rd_ptr(len);

	//sprintf(writer,"[%s]",strValue.c_str());
	//LOGDBG(writer);

	char tmpChar[defaultLength] = { 0 };
	memset(tmpChar, 0, defaultLength);
	int convertLength = CUnicodeConverter::UTF16Str_To_UTF8Str((const WORD*)(tmpBuf), (BYTE *)tmpChar);
	if (convertLength == 0) {
		printf("ByteArray::read_wstring error read string length =%d failed \n", len);
		return std::string("");
	}
	return std::string(tmpChar);*/
	return nullptr;
}

void * ByteArray::read_stream(int &mLength)
{
	mLength = read_int();
	void * readPtr = this->rd_ptr();
	rd_ptr(mLength);
	return readPtr;
}

void ByteArray::write_stream(void * mChars, int mLength)
{
	unsigned int len = this->space();
	unsigned int n = mLength + sizeof(int);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}

	write_int(mLength);
	copy((const char*)mChars, mLength);
}

void ByteArray::write_int(int value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(int);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	char conData[4] = { 0 };
	conData[3] = (char)(((value >> 24) & 0xFF));
	conData[2] = (char)(((value >> 16) & 0xFF));
	conData[1] = (char)(((value >> 8) & 0xFF));
	conData[0] = (char)((value & 0xFF));
	value = *((int *)conData);
	convertToByteStream(value);
	Write(value);
}

void ByteArray::write_uint(unsigned int value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(unsigned int);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	char conData[4] = { 0 };
	conData[3] = (char)(((value >> 24) & 0xFF));
	conData[2] = (char)(((value >> 16) & 0xFF));
	conData[1] = (char)(((value >> 8) & 0xFF));
	conData[0] = (char)((value & 0xFF));
	value = *((unsigned int *)conData);
	convertToByteStream(value);
	Write(value);
}

void ByteArray::write_int64(long long value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(long long);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	char conData[8] = { 0 };
	conData[7] = (char)(((value >> 56) & 0xFF));
	conData[6] = (char)(((value >> 48) & 0xFF));
	conData[5] = (char)(((value >> 40) & 0xFF));
	conData[4] = (char)(((value >> 32) & 0xFF));
	conData[3] = (char)(((value >> 24) & 0xFF));
	conData[2] = (char)(((value >> 16) & 0xFF));
	conData[1] = (char)(((value >> 8) & 0xFF));
	conData[0] = (char)((value & 0xFF));

	value = *((long long *)conData);
	convertToByteStream(value);
	Write(value);
}

void ByteArray::write_uint64(unsigned long long value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(unsigned long long);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	char conData[8] = { 0 };
	conData[7] = (char)(((value >> 56) & 0xFF));
	conData[6] = (char)(((value >> 48) & 0xFF));
	conData[5] = (char)(((value >> 40) & 0xFF));
	conData[4] = (char)(((value >> 32) & 0xFF));
	conData[3] = (char)(((value >> 24) & 0xFF));
	conData[2] = (char)(((value >> 16) & 0xFF));
	conData[1] = (char)(((value >> 8) & 0xFF));
	conData[0] = (char)((value & 0xFF));
	value = *((unsigned long long *)conData);
	convertToByteStream(value);
	Write(value);
}

void ByteArray::write_int16(short value)
{
	write_short(value);
}

void ByteArray::write_uint16(unsigned short value)
{
	write_ushort(value);
}

void ByteArray::write_short(short value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(short);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	char conData[2] = { 0 };
	conData[1] = (char)(((value >> 8) & 0xFF));
	conData[0] = (char)((value & 0xFF));
	value = *((short *)conData);
	convertToByteStream(value);
	Write(value);
}

void ByteArray::write_ushort(unsigned short value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(unsigned short);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	char conData[2] = { 0 };
	conData[1] = (char)(((value >> 8) & 0xFF));
	conData[0] = (char)((value & 0xFF));
	value = *((unsigned short *)conData);

	convertToByteStream(value);
	Write(value);
}

void ByteArray::write_long(long value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(long);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	char conData[4] = { 0 };
	conData[3] = (char)(((value >> 24) & 0xFF));
	conData[2] = (char)(((value >> 16) & 0xFF));
	conData[1] = (char)(((value >> 8) & 0xFF));
	conData[0] = (char)((value & 0xFF));
	value = *((long *)conData);
	convertToByteStream(value);
	Write(value);
}

void ByteArray::write_ulong(unsigned long value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(unsigned long);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	char conData[4] = { 0 };
	conData[3] = (char)(((value >> 24) & 0xFF));
	conData[2] = (char)(((value >> 16) & 0xFF));
	conData[1] = (char)(((value >> 8) & 0xFF));
	conData[0] = (char)((value & 0xFF));
	value = *((unsigned long *)conData);
	convertToByteStream(value);
	Write(value);
}

void ByteArray::write_char(signed char value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(char);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	Write(value);
}

void ByteArray::write_byte(signed char value)
{
	write_char(value);
}

void ByteArray::write_uchar(unsigned char value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(unsigned char);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	Write(value);
}

void ByteArray::write_bool(bool value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(bool);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}
	Write(value);
}

void ByteArray::write_float(float value)
{
	//	unsigned char* pBytes;
	//	pBytes = (unsigned char*)&value;
	//	char conData[4]={0};
	//	conData[3] = pBytes[3];
	//	conData[2] = pBytes[2];
	//	conData[1] = pBytes[1];
	//	conData[0] = pBytes[0];
	//	value = *((float *)conData);
	//	Write(value);
	unsigned int len = this->space();
	unsigned int n = sizeof(float);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}

	Write(value);
}

void ByteArray::write_double(double value)
{
	unsigned int len = this->space();
	unsigned int n = sizeof(double);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}

	Write(value);
}

#define UTF8_ASCII(byte) (((unsigned char)(byte)>=0x00)&&((unsigned char)(byte)<=0x7F))  
#define UTF8_FIRST(byte) (((unsigned char)(byte)>=0xC0)&&((unsigned char)(byte)<=0xFD))  
#define UTF8_OTHER(byte) (((unsigned char)(byte)>=0x80)&&((unsigned char)(byte)<=0xBF))  
static int _calcCharCount(const char * pszText, int len)
{
	char *p = 0;
	long count = 0;

	if (!pszText || len <= 0) {
		return 0;
	}

	for (p = (char*)pszText; p<pszText + len; p++) {
		if (UTF8_ASCII(*p) || (UTF8_FIRST(*p))) {
			count++;
		}
	}

	return count;
}

static std::string utf8_substr(const std::string& str, unsigned long start, unsigned long leng)
{
	if (leng == 0)
	{
		return "";
	}
	unsigned long c, i, ix, q, min = std::string::npos, max = std::string::npos;
	for (q = 0, i = 0, ix = str.length(); i < ix; i++, q++)
	{
		if (q == start)
		{
			min = i;
		}
		if (q <= start + leng || leng == std::string::npos)
		{
			max = i;
		}

		c = (unsigned char)str[i];

		if (c <= 127) i += 0;
		else if ((c & 0xE0) == 0xC0) i += 1;
		else if ((c & 0xF0) == 0xE0) i += 2;
		else if ((c & 0xF8) == 0xF0) i += 3;
		else return "";//invalid utf8
	}
	if (q <= start + leng || leng == std::string::npos)
	{
		max = i;
	}
	if (min == std::string::npos || max == std::string::npos)
	{
		return "";
	}
	return str.substr(min, max);
}

void  ByteArray::write_string(const char* str)
{
	std::string input_text = str;
	/*
	CCLOG("%s", input_text);
	long input_count = StringUtils::getCharacterCountInUTF8String(str);
	input_text = cocos2d::ui::Helper::getSubStringOfUTF8String(input_text, 0, input_count);
	int len = input_text.length();
	std::string insert(input_text, len);
	CCLOG("%d,%d,%s", input_count, len, input_text);


	int stringLength = _calcCharCount(input_text.c_str(), input_text.length());
	std::string text = utf8_substr(input_text, 0, input_text.length());	*/
	write_string(input_text);
}

void  ByteArray::write_string(const std::string& str)
{
	std::string data = str;
	write_short(static_cast<unsigned short>(data.length()));
	copy(data.data(), data.length());

	//    const int defaultLength = 65535;
	//	unsigned short tmpBuf[defaultLength]= {0};
	//	memset(tmpBuf, 0, defaultLength);
	//	
	//	int resultLen = CUnicodeConverter::UTF8StrToUnicode(str.c_str(),str.length(),tmpBuf,defaultLength);
	//	if (resultLen != 0)
	//    {
	//		write_int(resultLen);
	//        for (int i=0; i<resultLen; i++) {
	//            write_short(tmpBuf[i]);
	//        }
	//	}
	//    else {
	//        write_short(static_cast<unsigned short>(data.length()));
	//        copy(data.data(), data.length());
	//    }
}

void  ByteArray::write_wstring(const std::string& str)
{
	/**
	const int defaultLength = 65535;
	unsigned short tmpBuf[defaultLength]= {0};
	memset(tmpBuf, 0, defaultLength);

	int resultLen = CUnicodeconvertToByteStream::UTF8StrToUnicode(str.c_str(),str.length(),tmpBuf,defaultLength);
	if (resultLen != 0) {
	int writeLength = resultLen * 2;//unicode is always 2 character
	write_short(static_cast<unsigned short>(writeLength));
	this->copy((const char *)tmpBuf, writeLength );
	}
	else {
	log("ByteArray::write_wstring error \n");
	}


	unsigned int len = this->space();
	unsigned int n = str.length() + sizeof(unsigned short);
	if (len < n)
	{
		this->resize(n + m_nTotalSize);
	}

	const int defaultLength = 65535;
	unsigned short tmpBuf[defaultLength] = { 0 };
	memset(tmpBuf, 0, defaultLength);

	int resultLen = CUnicodeConverter::UTF8StrToUnicode(str.c_str(), str.length(), tmpBuf, defaultLength);
	if (resultLen >= 0) {
		int writeLength = resultLen * 2;//unicode is always 2 character
		write_uint16(static_cast<unsigned short>(writeLength));
		this->copy((const char *)tmpBuf, writeLength);
	}
	else {
		printf("ByteArray::write_wstring error \n");
	}	**/
}

void ByteArray::write_chars(const char* str)
{
	for (unsigned i = 0; i<m_nTotalSize; i++)
	{
		write_char(str[i]);
	}
}

unsigned char* ByteArray::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
	unsigned char * pBuffer = NULL;
	CCAssert(pszFileName != NULL && pSize != NULL && pszMode != NULL, "Invalid parameters.");
	*pSize = 0;
	do
	{
		// read the file from hardware
		std::string fullPath = pszFileName;
		FILE *fp = fopen(fullPath.c_str(), pszMode);
		CC_BREAK_IF(!fp);

		fseek(fp, 0, SEEK_END);
		*pSize = ftell(fp);
		fseek(fp, 0, SEEK_SET);
		pBuffer = new unsigned char[*pSize];
		*pSize = fread(pBuffer, sizeof(unsigned char), *pSize, fp);
		fclose(fp);
	} while (0);

	if (!pBuffer)
	{
		std::string msg = "Get data from file(";
		msg.append(pszFileName).append(") failed!");

		CCLOG("%s", msg.c_str());
	}
	//cout << "pBuffer:===============" << pBuffer << endl;
	return pBuffer;
}
